package cn.gary.oaserver.config.security;

import cn.gary.oaserver.params.Result;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author: 邓必宏
 * @title:
 * @Demo:
 * @DateTime: 2024/3/18
 */
public class MyAccessDeniedHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse response, AccessDeniedException e) throws IOException, ServletException {
        response.setContentType ( "application/json;charset=utf-8" );
        PrintWriter out = response.getWriter ( );
        out.write ( new ObjectMapper (  ).writeValueAsString ( Result.error ( 403,"无权访问此请求",null ) ) );
        out.flush ();
        out.close ();
        //傻逼老邓
    }
}
